package com.dubroy.mnemobot;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class StudyActivity extends Activity {
	private static final String LOG_TAG = "com.dubroy.Flashcards";

	class Flashcard extends LinearLayout implements OnClickListener {
		public Flashcard(Context context, OnClickListener buttonListener, String question, String answer) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.flashcard, this, true);
			setOnClickListener(this);
			
			TextView answerText = (TextView)findViewById(R.id.answerText);
			TextView questionText = (TextView)findViewById(R.id.questionText);
			
			questionText.setText(question);
			answerText.setText(answer);
			
			// Add event handlers for all the buttons
			ViewGroup buttonLayout = (ViewGroup)findViewById(R.id.buttonLayout);
			for (int i = 0; i < buttonLayout.getChildCount(); i++) {
				View button = (View)buttonLayout.getChildAt(i);
				button.setOnClickListener(buttonListener);
			}

			showQuestion();
		}

		public void onClick(View v) {
			TextView questionText = (TextView)findViewById(R.id.questionText);
			View answerView = findViewById(R.id.answerView);
			int cx = this.getWidth() / 2;
			int cy = this.getHeight() / 2;
			Flip3dAnimation animation = new Flip3dAnimation(questionText,
					answerView, cx, cy);
			if (findViewById(R.id.questionText).getVisibility() == View.GONE) {
				animation.reverse();
			}
			startAnimation(animation);
			Log.v(LOG_TAG, "Qustion: " + questionText.getText());
		}
		
		public void showQuestion() {
			findViewById(R.id.questionText).setVisibility(VISIBLE);
			findViewById(R.id.answerView).setVisibility(GONE);			
		}
		
		public void showAnswer() {
			findViewById(R.id.questionText).setVisibility(GONE);
			findViewById(R.id.answerView).setVisibility(VISIBLE);			
		}
	}
	
	private void nextCard() {
		ViewFlipper cardFlipper = (ViewFlipper) findViewById(R.id.cardFlipper);
		cardFlipper.setOutAnimation(outToLeftAnimation());
		cardFlipper.setInAnimation(inFromRightAnimation());

		int currentIndex = cardFlipper.getDisplayedChild();
		int nextIndex = (currentIndex + 1) % cardFlipper.getChildCount();
		Flashcard nextCard = (Flashcard)cardFlipper.getChildAt(nextIndex);
		nextCard.showQuestion();
		
		cardFlipper.showNext();
	}
	
	private void previousCard() {
	}

	private Animation inFromRightAnimation() {
		Animation anim = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		anim.setDuration(500);
		anim.setInterpolator(new AccelerateInterpolator());
		return anim;
	}

	private Animation outToLeftAnimation() {
		Animation anim = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		anim.setDuration(500);
		anim.setInterpolator(new AccelerateInterpolator());
		return anim;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.study);
		final ViewFlipper cardFlipper = (ViewFlipper) findViewById(R.id.cardFlipper);

		OnClickListener buttonListener = new OnClickListener() {
			public void onClick(View v) {
				nextCard();
			}
		};
		Flashcard card1 = new Flashcard(getApplicationContext(),
				buttonListener,
				"boat",
				"das Boot");
		Flashcard card2 = new Flashcard(getApplicationContext(),
				buttonListener,
				"airport",
				"der Flughafen");
		Flashcard card3 = new Flashcard(getApplicationContext(),
				buttonListener,
				"train station",
				"der Bahnhof");
		cardFlipper.addView(card1);
		cardFlipper.addView(card2);
		cardFlipper.addView(card3);
	}
}